package com.thinkgem.jeesite.common.utils;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import com.alibaba.fastjson.JSON;



/**
 * 微信预警工具类
 * 
 * @author WangYuan
 * @date 2016年8月1日下午6:01:19
 */
public class WxSenderUtil {

    WxSenderUtil() {}

    /**
     * 
     * @param content 发送内容
     * @param wxNumber 接收微信号
     * @param corpid
     * @param corpsecret
     * @param tokenUrl
     * @param sendUrl
     * @return
     * @throws Exception
     */
    public static String sendMonitorMsg(String content, String wxNumber, String corpid,
            String corpsecret, String tokenUrl, String sendUrl) throws Exception {
        Map<String, String> params = new HashMap<>();
        params.put("corpid", corpid);
        params.put("corpsecret", corpsecret);
        String tokenStr = HttpClientUtil.getReq(tokenUrl, params);
        JSONObject json = new JSONObject(tokenStr);
        String token = json.getString("access_token");

        Map<String, Object> postMap = new HashMap<>();
        Map<String, Object> contentMap = new HashMap<>();
        contentMap.put("content", content);
        postMap.put("safe", "0");
        postMap.put("agentid", 1);
        postMap.put("msgtype", "text");
        postMap.put("touser", wxNumber);
        postMap.put("text", contentMap);
        String postData = JSON.toJSONString(postMap);
        String postUrl = sendUrl + "?access_token=" + token;
        return HttpClientUtil.postReq(postUrl, postData);
    }
}
